// pages/user/info/index.js
const app = getApp()
Page({
  data: {
    userInfo: null,
    spinShow: false,
    levelIndex: 0
  },
  onLoad: function(options) {
    this.loadUserInfo()
  },
  loadUserInfo() {
    let _this = this
    _this.setData({
      spinShow: true
    });
    app.formPost('/api/wx/student/user/current', null).then(res => {
      if (res.code == 1) {
        _this.setData({
          userInfo: res.response,
          levelIndex: res.response.userLevel-1
        });
      }else if(res.code == 401){

        wx.qy.login({
            success(wxres) {
                console.log("code是"+wxres.code)

              if (wxres.code) {
                app.formGet('/inApi/qywx/student/auth/appletLogin', {
                  "code": wxres.code
                }).then(res => {
                  if (res.code == 1) {
                    wx.setStorageSync('token', res.response.token)
                    wx.reLaunch({
                      url: '/pages/index/index',
                    });
                  }
                  else {
                    _this.message(res.message, '您无该系统的权限，请联系系统管理员')
    
                        wx.reLaunch({
                      url: '/pages/403/index',
                    });
                  }
                }).catch(e => {
                  _this.message(e, 'error')
                })  
              } else {
                _this.message(res.errMsg, 'error')
              }
            }
          })

      }else{
          
      }
      _this.setData({
        spinShow: false
      });
    }).catch(e => {
      _this.setData({
        spinShow: false
      });
      app.message(e, 'error')
    })
  },
  bindLevelChange: function(e) {
    this.setData({
      levelIndex: e.detail.value
    })
  },
  bindDateChange(e) {
    let {
      value
    } = e.detail;
    this.setData({
      "userInfo.birthDay": value
    })
  },
  formSubmit: function(e) {
    let _this = this
    wx.showLoading({
      title: '提交中',
      mask: true
    })
    app.formPost('/api/wx/student/user/update', e.detail.value)
      .then(res => {
        if (res.code == 1) {
          wx.reLaunch({
            url: '/pages/my/index/index',
          });
        }else if(res.code == 401){

            wx.qy.login({
                success(wxres) {
                    console.log("code是"+wxres.code)
    
                  if (wxres.code) {
                    app.formGet('/inApi/qywx/student/auth/appletLogin', {
                      "code": wxres.code
                    }).then(res => {
                      if (res.code == 1) {
                        wx.setStorageSync('token', res.response.token)
                        wx.reLaunch({
                          url: '/pages/index/index',
                        });
                      }
                      else {
                        _this.message(res.message, '您无该系统的权限，请联系系统管理员')
        
                            wx.reLaunch({
                          url: '/pages/403/index',
                        });
                      }
                    }).catch(e => {
                      _this.message(e, 'error')
                    })  
                  } else {
                    _this.message(res.errMsg, 'error')
                  }
                }
              })
    
          } else {
          app.message(res.message, 'error')
        }
        wx.hideLoading()
      }).catch(e => {
        app.message(e, 'error')
        wx.hideLoading()
      })
  }
})